//{{define "script"}}


function user_set(cl,nick,target)
{
    $.ajax({
        type: "POST",
        url: "/action/user",
        async: false,
        data: {
            "action": "set",
            "class": cl,
            "nick": nick,
            "target": target
        }
    });
}


function user_save_click(t){
    //$('#modifyModal_'+t).modal('hide');
    $target = $('div#modifyModal_'+t);
    $target.modal('hide');
    cl=$target.find(".input_class").val();
    nick=$target.find(".input_nick").val();
    $target.on('hidden.bs.modal', function (e) {
        user_set(cl, nick, t);
        load();
    })
}




//载入/更新用户列表
function load_users() {
    //$("#users_list").html("<span class=\"label label-info\">载入中...</span>");
    htmlobj = $.ajax({
        type: "GET",
        url: "/action/user",
        async: false,
        data: {
            "action": "load"
        }
    });
    $("#users_list").html(htmlobj.responseText);
    ready_user_fucntion();
}



function load_dynamic() {
    //$("#announce_list").html("<span class=\"label label-info\">载入中...</span>");
    htmlobj = $.ajax({
        type: "GET",
        url: "/action/announce",
        async: false,
        data: {
            "action": "dynamic"
        }
    });
    $("#announce_add_content").val("");
    $("#announce_list").html(htmlobj.responseText);
    ready_dynamic_fucntion();
}


//载入/更新TODO列表
function load_todo() {
    //$("#todo_list").html("<span class=\"label label-info\">载入中...</span>");
    htmlobj = $.ajax({
        type: "GET",
        url: "/action/todo",
        async: false,
        data: {
            //{{if .IsLook}}
            "action": "look",
            "id":{{.WhoId}}
            //{{else}}
            "action": "load"
            //{{end}}
        }
    });

    $("#todo_add_text").val("");
    $("#todo_list").html(htmlobj.responseText);
    $('#todo_more_option').slideUp();
    ready_todo_fucntion();

    //{{if .IsLook}}
    $("button.todo_done").remove();
    $("button.todo_delete").remove();
    //{{else}}
    //{{end}}
}

function set_class(cls, target, cls_name) {

    var c = parseInt(cls, 10);
    $("#class_name_" + target).text(cls_name);
    $("#modifyModal_" + target).find("input.input_class").attr({
        "value": (c).toString()
    });
}


//添加TODO时设置的等级
function set_level(level) {
    var l = parseInt(level, 10);
    $("div#todo_level").text((l).toString());
    $("input#todo_add_level").attr({
        "value": (l - 1).toString()
    });
}
//全部完成TODO异步请求
function todo_all_done() {
    $.ajax({
        type: "post",
        url: "/action/todo",
        async: false,
        data: {
            "action": "alldone"
        }
    });
    load();
    return false;
}

//全部重置(取消完成)TODO异步请求
function todo_all_undone() {
    $.ajax({
        type: "post",
        url: "/action/todo",
        async: false,
        data: {
            "action": "allundone"
        }
    });
    load();
    return false;
}
//全部移除完成TODO异步请求
function todo_clean() {
    $.ajax({
        type: "post",
        url: "/action/todo",
        async: false,
        data: {
            "action": "allremovedone"
        }
    });
    load();
    return false;
}

function ready_todo_fucntion() {
    //删除TODO异步请求
    $('button.todo_delete').click(function() {
        $.ajax({
            type: "post",
            url: "/action/todo",
            data: {
                "action": "remove",
                "id": $(this).parent().parent().attr("id")
            }
        });
        $(this).parent().parent().remove();
        return false;
    });
    //完成TODO异步请求
    $('button.todo_done').click(function() {
        $.ajax({
            type: "post",
            url: "/action/todo",
            async: false,
            data: {
                "action": "done",
                "id": $(this).parent().parent().attr("id")
            }
        });
        load();
        return false;
    });
    //重置(取消完成)TODO异步请求
    $('button.todo_undone').click(function() {
        $.ajax({
            type: "post",
            url: "/action/todo",
            async: false,
            data: {
                "action": "undone",
                "id": $(this).parent().parent().attr("id")
            }
        });
        load();
        return false;
    });
}

function ready_dynamic_fucntion() {
    //删除announce异步请求
    $('button.announce_delete').click(function() {
        $.ajax({
            type: "post",
            url: "/action/announce",
            data: {
                "action": "remove",
                "id": $(this).parent().parent().attr("id")
            }
        });
        $(this).parent().parent().remove();
        return false;
    });
    //评论announce异步请求
    $('button.announce_comment').click(function() {
        $.ajax({
            type: "post",
            url: "/action/comment",
            async: false,
            data: {
                "action": "add",
                "type": "announce",
                "id": $(this).parent().parent().attr("id")
            }
        });
        load();
        return false;
    });

}

function ready_user_fucntion() {
    //查看TODO
    $('button.user_look').click(function() {
        document.location.href = "/?page=todo&id=" + $(this).parent().parent().attr("id");
        return false;
    });

    //删除用户
    $('button.user_delete').click(function() {
        $.ajax({
            type: "post",
            url: "/action/user",
            async: false,
            data: {
                "action": "remove",
                "id": $(this).attr("target-id")
            }
        });
        load();
        return false;
    });
/*
    $('tr.user_info_row').next().hide();
    $('tr.user_info_row').mouseleave(function(event) {
        $(this).next().slideUp('400');

    });
    $('tr.user_info_row').mouseenter(function(event) {
        $(this).next().slideDown('400');
    });
    */
    
}
/*
//载入/更新用户列表
function load_users() {
    //$("#users_list").html("<span class=\"label label-info\">载入中...</span>");
    htmlobj = $.ajax({
        type: "GET",
        url: "/action/user",
        async: false,
        data: {
            "action": "load"
        }
    });
    $("#todo_add_text").val("");
    $("#todo_list").html(htmlobj.responseText);
    $('#todo_more_option').slideUp();
    ready_todo_fucntion();
}
*/
function load() {
    //{{if .IsTodo }}
    load_todo();
    //{{end}}
    //{{if .IsDynamic }}
    load_dynamic();
    //{{end}}
    //{{if .IsUsers }}
    load_users();
    //{{end}}
}

$(document).ready(function() {
    //载入动态信息
    load();
    //添加TODO的文本框下拉选项显示
    $('#todo_more_option').hide();
    $('#side_board').click(function() {
        $('#todo_more_option').slideDown();
    });
    //导航点击高亮
    $("a.link").click(function() {
        $("a.link").removeClass("active");
        $(this).addClass("active");
    });
    //添加TODO异步请求
    $('#btn_todo_add').click(function() {
        $form = $(this);
        $.ajax({
            type: "POST",
            url: "/action/todo",
            async: false,
            data: {
                "action": "add",
                "text": $("#todo_add_text").val(),
                "level": $("#todo_add_level").attr("value")
            }
        });
        load();
    });

    //发布广播异步请求
    $('#btn_publish_announce').click(function() {
        $form = $(this);
        $.ajax({
            type: "POST",
            url: "/action/announce",
            async: false,
            data: {
                "action": "add",
                "text": $("#announce_add_content").val()
            }
        });
        load();
    });



});

//{{end}}